# !/bin/sh
# Delete file which is in dest dir but not in source dir

if [ -z "$VOLUME_BACKUP" -o -z "$BASENAME" ];
then
	echo "Error: env var not set!!!" 1>&2
	exit 1
fi

srcdir=`pwd`
destdir=$VOLUME_BACKUP/$BASENAME

tmpfile=/tmp/filelist.log

if [ "`pwd`" = "$destdir" ];
then
	echo "Error: source dir is the same as destnation dir!!!" 1>&2
	exit 0
fi

cd "$destdir" && find . -name "*" -type f -print0 | xargs -0 printf "%s\n" > $tmpfile
cat $tmpfile | while read bfile
do
	if [ -z "$bfile" ];
	then
		continue
	fi

	if [ ! -e "$srcdir"/"$bfile" ];
	then
		echo "Delete file: $destdir/$bfile"
		if [ "$forcedelete" == "true" ];
		then
			rm "$bfile"
		else
			mv "$bfile" $TRASH
		fi
	else
		echo "File exists in source dir: $bfile"
	fi
done
sync
rm $tmpfile

